package net.huashitong.supervision.AppNew.appCtrl;

import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.AppNew.DTO.UserDTO;
import net.huashitong.supervision.AppNew.appService.AppInsideService;
import net.huashitong.supervision.insideDuCha.entity.InsideMainTaskEntity;
import net.huashitong.supervision.insideDuCha.entity.InsideSubTaskEntity;
import net.huashitong.supervision.insideDuCha.entity.InsideUserTaskEntity;
import net.huashitong.supervision.insideDuCha.filter.InsideMainTaskFilter;
import net.huashitong.supervision.insideDuCha.filter.InsideSubTaskFilter;
import net.huashitong.supervision.insideDuCha.filter.InsideUserTaskFilter;
import net.huashitong.supervision.insideDuCha.service.InsideMainTaskService;
import net.huashitong.supervision.insideDuCha.service.InsideSubTaskService;
import net.huashitong.supervision.insideDuCha.service.InsideUserTaskService;
import net.huashitong.supervision.task.entity.TaskBackNew;
import net.huashitong.supervision.task.entity.TaskPermissions;
import net.huashitong.supervision.task.filter.TaskBackNewFilter;
import net.huashitong.supervision.task.service.TaskBackNewService;
import net.huashitong.supervision.task.service.TaskPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/app/inside")
public class AppInsideCtrl extends JsonBaseController {

    @Autowired
    private InsideMainTaskService mainService;
    @Autowired
    private InsideUserTaskService userService;
    @Autowired
    private TaskBackNewService backNewService;
    @Autowired
    private InsideSubTaskService subTaskService;

    @Autowired
    private AppInsideService insideService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private TaskPermissionService taskPermissionService;



    @Override
    protected String getView(String s) {
        return null;
    }


    private UserDTO getUserInfo(String loginId){
        String jsonUser = redisService.get("applogin:dc:" + loginId);
        UserDTO userDTO = JSONObject.parseObject(jsonUser, UserDTO.class);
        return userDTO;
    }

    /**
     * app 单位内部立项
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/setup",method = RequestMethod.POST)
    public Message saveInsideTask(@RequestBody InsideMainTaskFilter filter)
    {

        String loginId = filter.getLoginerId();
        UserDTO userInfo = getUserInfo(loginId);
        filter.setDeptId(userInfo.getDeptId());

//        filter.setTimeType(1);//设置成单时限
        List<InsideSubTaskFilter> insideSubTasks = filter.getInsideSubTasks();
        insideSubTasks.forEach(filter1 -> filter1.setTimeType(1));
        filter.setInsideSubTasks(insideSubTasks);//全部设置成单时限
        List<InsideSubTaskFilter> filters = mainService.saveTask(filter);
        return coverMessage("200", "立项成功");
    }

    /**
     * 反馈
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/feedback",method = RequestMethod.POST)
    public Message feedbackTask(@RequestBody TaskBackNewFilter filter)
    {

        String loginerId = filter.getLoginerId();
        UserDTO userInfo = getUserInfo(loginerId);

        InsideUserTaskEntity userTask = userService.getById(filter.getUserTaskId());

        TaskBackNew entity = new TaskBackNew();

        entity.setStatus("3");
        entity.setTaskUnitId(userTask.getSysId());//人员任务id
        entity.setTaskMainId(userTask.getMainTaskId());
        entity.setTaskSubId(userTask.getSubTaskId());
        entity.setSenderName(userInfo.getRealName());
        entity.setContent(filter.getContent());
        entity.setProblem(filter.getProblem());
        entity.setPlan(filter.getPlan());
        entity.setFileName(filter.getFileName());
//        entity.setFile(filter.getFileIds());
        entity.setDeptType("5");  //1 牵头， 2 协办 ，3 督办单位；4 攻坚任务 5 内部
        entity.setDel(null);

        backNewService.save(entity,loginerId);
        //反馈后主任务 和子任务 装填更新为 新反馈

        InsideMainTaskEntity main = mainService.getById(userTask.getMainTaskId());
        main.setBackType("1");
        main.setTaskState("2");
        mainService.update(main);
        //subTask.setBackType("1");
        InsideSubTaskEntity subTask = subTaskService.getById(userTask.getSubTaskId());
       /* subTask.setTaskState("2");//2在办  type /任务状态(0.待签收,1待反馈,2已反馈,3退回,4完成 。5已签收)
        subTaskService.update(subTask);*/
        InsideUserTaskEntity userTaskEntity = userService.getById(userTask.getSysId());
        userTaskEntity.setTaskState("2"); //type /任务状态(0.待签收,1待反馈,2已反馈,3退回,4完成 。5已签收)
        userService.update(userTaskEntity);

        return coverMessage("200","反馈成功");
    }


    /**
     * 设置任务完成
     */
    @ResponseBody
    @RequestMapping(value = "/finishTask",method = RequestMethod.POST)
    public  Message finishTask(@RequestBody TaskBackNewFilter filter )
    {
        String loginerId = filter.getLoginerId();
        UserDTO userInfo = getUserInfo(loginerId);

        TaskBackNew taskBackNew = backNewService.getById(filter.getSysId());
        if(ObjectUtils.isBlank(taskBackNew))
        {
            return  coverMessage("500","操作错误，找到该反馈信息");
        }

        String createUser = taskBackNew.getCreateUser();
        if(createUser.equals(userInfo.getUserId())){
            return coverMessage("500","对不起，您自己不能办结");
        }
        String taskUnitId = taskBackNew.getTaskUnitId();
        InsideUserTaskEntity userTask = userService.getById(taskUnitId);
        if("4".equals(userTask.getTaskState())){
            return  coverMessage("500","办结失败，任务不能重复办结");
        }
        userTask.setTaskState(4+"");
        userService.update(userTask);
        return  coverMessage("200","任务办结成功");
    }


    /**
     * 查询任务列表
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/taskList",method = RequestMethod.POST)
    public Message insideTasks(@RequestBody InsideUserTaskFilter filter)
    {
        String loginerId = filter.getLoginerId();
        UserDTO userInfo = getUserInfo(loginerId);
        Paging paging = filter.getPaging();
        if(ObjectUtils.isBlank(paging))
        {
            paging= new Paging();
            paging.setCurrentPage(1);
            paging.setPageSize(10);//默认10条
            filter.setPaging(paging);
        }
        //        任务状态 0 未查看 1 未反馈，2 已反馈，3 逾期， 4 完成   9 我创建的 ，10 全部  11 未完成

        if(!"9".equals(filter.getTaskState())){

//                filter.setCreateUser(filter.getLoginerId());
            filter.setUserId(filter.getLoginerId());
            filter.setTaskState(filter.getTaskState());
        }
        TaskPermissions per = taskPermissionService.getPer(loginerId, 2);
        if(ObjectUtils.isNotBlank(per))
        {
            filter.setDeptId(userInfo.getDeptId());
            filter.setCreateUser(null);
            filter.setUserId(null);

        }else {

            filter.setCreateUser(filter.getLoginerId());
            filter.setDeptId(null);
        }


        List<InsideUserTaskFilter> taskLists = insideService.getTaskLists(filter);
        int records =0;
        records=filter.getPaging().getRecords();
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",taskLists);
        map.put("total",records);

        return coverMessage("200", "内部任务列表返回成功", map);

    }

    /**
     * 获取反馈记录
     */

    @ResponseBody
    @RequestMapping(value = "/insideBack",method = RequestMethod.POST)
    public  Message insideBackfeed(@RequestBody TaskBackNewFilter filter )
    {

        List<TaskBackNew> backList = backNewService.getByTaskUnitId(filter.getUserTaskId());

        return coverMessage("200","获取反馈记录",backList);

    }


    /**
     * 获取同单位下的人员
     */
    @ResponseBody
    @RequestMapping(value = "/sameDeptUser",method = RequestMethod.GET)
    public  Message getSameDeptUser(String loginerId)
    {

        UserDTO userInfo = getUserInfo(loginerId);
        SysUserFilter filter = new SysUserFilter();
        filter.setDeptId(userInfo.getDeptId());
        List<SysUserDataBean> list = sysUserService.queryOnDatabeanNoPage(filter);
        list.forEach(o->
                {        o.setUserId(o.getSysId());
                        o.setPassword("");
                }

        );

        return coverMessage("200","获取反馈记录",list);

    }

    /**
     * 改变任务状态
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/changstatue",method = RequestMethod.POST)
    public Message  changeTaskstatue(@RequestBody InsideUserTaskFilter filter){
        String sysId = filter.getSysId();
        String loginerId = filter.getLoginerId();
        if(StringUtils.isBlank(sysId)||StringUtils.isBlank(loginerId))
        {
            return coverMessage("500","单位任务id和登陆用户不能为空");

        }

        InsideUserTaskEntity userTask = userService.getById(sysId);
        String taskState = userTask.getTaskState();
        if("2".equals(taskState)||"3".equals(taskState)||"4".equals(taskState))
        {
            return coverMessage("200","更新成功");
        }else {
            if(userTask.getUserId().equals(loginerId)){

                userTask.setTaskState(1+"");
                userService.update(userTask);
            }
        }


        return coverMessage("200","更新成功");


    }


}
